package kr.co.choongang;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HomeController {
	private static final Logger logger = LoggerFactory
			.getLogger(HomeController.class);

	ArrayList<Board> list = new ArrayList<Board>();
	IBoardDAO ibaties = new IBoardDAO();

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,
				DateFormat.LONG, locale);
		String formattedDate = dateFormat.format(date);
		model.addAttribute("serverTime", formattedDate);
		return "board_main";
	}

	@RequestMapping(value = "board_main", method = RequestMethod.GET)
	public ModelAndView boardmain() {
		List<Board> list = ibaties.selectBoard();
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("boardList", list);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("board_main");
		mav.addObject("boardList", list);
		return mav;
	}

	@RequestMapping("write")
	public ModelAndView write() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("board_write");
		modelAndView.addObject(new Board());
		return modelAndView;
	}

	@RequestMapping(value = "insert", method = RequestMethod.POST)
	public String boardwrite(Board boardDto, HttpServletRequest request) {
		System.out.println(boardDto);
		boardDto.setBoard_readacount(0);
		ibaties.insertboard(boardDto);
		return "redirect:board_main";
	}

	@RequestMapping(value = "board_detail", method = RequestMethod.GET)
	public ModelAndView boarddetail(Integer board_no,
			HttpServletRequest request, HttpServletResponse response) {
		ibaties.updateReadCount(board_no);
		Board board = ibaties.selectfour(board_no);
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("Board", board);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("board_detail");
		modelAndView.addObject("Board", board);
		return modelAndView;
	}

	@RequestMapping(value = "update", method = RequestMethod.GET)
	public ModelAndView update(Integer board_no) {
		Board board = ibaties.selectfour(board_no);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("board_update");
		modelAndView.addObject("Board", board);
		return modelAndView;
	}

	@RequestMapping(value = "updateb", method = RequestMethod.POST)
	public String updateBoard(Integer board_no, Board boardDto,
			HttpServletRequest request) {
		ibaties.updateboard(boardDto);
		System.out.println(boardDto);
		return "redirect:board_main";
	}

	@RequestMapping(value = "delete", method = RequestMethod.GET)
	public String delete(Integer board_no, HttpServletRequest request) {
		ibaties.deleteboard(board_no);
		return "redirect:board_main";
	}
	
	@RequestMapping(value = "search", method = RequestMethod.POST)
	public ModelAndView search(String board_title) {
		if (board_title == null || board_title.equals("")) {
			return this.boardmain();
		}
		System.out.println(board_title);
		ArrayList<Board> list =  ibaties.selecttitle(board_title);
		if (list == null) {
			return this.boardmain();
		}

		ModelAndView mav = new ModelAndView();
		mav.setViewName("board_main");
		mav.addObject("boardList", list);
		return mav;
	}
}
